<script setup>
import {ref} from "vue";
import {getCommodity} from "@/api/ShopApi.js";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";

const commodities = ref([]);

// 查询商品
getCommodity().then(resp=>{
  commodities.value = resp.data;
}).catch(resp=>{
  ElMessage({
    message: '查询失败！',
    type: 'warning'
  });
});
</script>
<template>
  <div class="mall-container">
    <div class="commodity-grid">
      <div v-for="commodity in commodities" 
           class="commodity-card" 
           :title="commodity.name" 
           @click="router.push('/commodity?id='+commodity.id)">
        <div class="commodity-image-container">
          <img class="commodity-image" :src="commodity.coverUrl">
          <div class="commodity-tags">
            <span v-if="commodity.salesType==='seckill'" class="tag tag-seckill">秒杀</span>
            <span v-else-if="commodity.salesType==='acution'" class="tag tag-auction">拍卖</span>
            <span v-else-if="commodity.shopId===1" class="tag tag-self">自营</span>
          </div>
        </div>
        <div class="commodity-info">
          <div class="commodity-name">
            {{commodity.name}}
          </div>
          <div class="commodity-price">
            <span class="price-symbol">￥</span>
            <span class="price-value">{{commodity.price}}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
  
  <div class="shop-menu">
    <div class="shop-menu-item" @click="href('/center?a=2&b=1')">
      <i class="el-icon-shopping-cart-full"></i>
      <span class="menu-text">购物车</span>
    </div>
    <div class="shop-menu-item" @click="href('/center?a=2&b=2')">
      <i class="el-icon-s-order"></i>
      <span class="menu-text">我的订单</span>
    </div>
    <div class="shop-menu-item" @click="href('/center?a=2&b=3')">
      <i class="el-icon-s-shop"></i>
      <span class="menu-text">我的商铺</span>
    </div>
  </div>
  
  <el-backtop :right="100" :bottom="100" :visibility-height="1" class="custom-backtop">
    <div class="backtop-inner">
      顶部
    </div>
  </el-backtop>
</template>
<style scoped>
::-webkit-scrollbar {
  display: none;
}

.mall-container {
  max-width: 1200px;
  margin: 20px auto;
  padding: 0 15px;
}

.commodity-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 15px;
}

.commodity-card {
  border-radius: 6px;
  overflow: hidden;
  background-color: #fff;
  box-shadow: 0 2px 8px 0 rgba(0, 0, 0, 0.08);
  transition: all 0.3s ease;
  cursor: pointer;
}

.commodity-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.commodity-image-container {
  position: relative;
  width: 100%;
  height: 180px;
  overflow: hidden;
}

.commodity-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.5s ease;
}

.commodity-card:hover .commodity-image {
  transform: scale(1.05);
}

.commodity-tags {
  position: absolute;
  top: 10px;
  left: 10px;
  display: flex;
  gap: 5px;
}

.tag {
  padding: 2px 8px;
  border-radius: 4px;
  font-size: 12px;
  color: white;
  font-weight: 500;
}

.tag-seckill {
  background-color: #E3251B;
}

.tag-auction {
  background-color: #e1ac1b;
}

.tag-self {
  background-color: #E3251B;
}

.commodity-info {
  padding: 8px 10px;
}

.commodity-name {
  font-size: 13px;
  line-height: 1.3;
  color: #333;
  height: 34px;
  overflow: hidden;
  margin-bottom: 6px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  text-overflow: ellipsis;
}

.commodity-price {
  color: #E3251B;
  font-weight: 700;
  font-family: arial, sans-serif;
  display: flex;
  align-items: baseline;
}

.price-symbol {
  font-size: 14px;
  margin-right: 2px;
}

.price-value {
  font-size: 20px;
}

.shop-menu {
  position: fixed;
  top: 50%;
  right: 0;
  transform: translateY(-50%);
  background-color: #fff;
  border-radius: 8px 0 0 8px;
  box-shadow: -2px 0 10px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  z-index: 999;
}

.shop-menu-item {
  padding: 15px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.shop-menu-item:last-child {
  border-bottom: none;
}

.shop-menu-item:hover {
  background-color: #f5f5f5;
  color: #E3251B;
}

.menu-text {
  font-size: 12px;
  margin-top: 5px;
  writing-mode: vertical-lr;
  text-orientation: upright;
}

.custom-backtop {
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 50%;
  transition: all 0.3s;
}

.custom-backtop:hover {
  background-color: #f5f5f5;
}

.backtop-inner {
  height: 100%;
  width: 100%;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
  color: #1989fa;
}
</style>